﻿myApp.controller('PropertyDetailController', function ($scope, $http, $routeParams,$filter,NgMap,$location, $rootScope) {
  
    $scope.isSearch = true;
    $scope.isAndroid = $rootScope.isAndroid;


    var BASE_URL;
    if ($location.host() == "localhost") {
        BASE_URL = $location.protocol() + "://" + "localhost:" + $location.port();
    } else {
        BASE_URL = $location.protocol() + "://" + $location.host();
    }

    $scope.current_path_url = $location.absUrl();

    NgMap.getMap().then(function (map) {
        $scope.map = map;
    });

    $scope.mouseover = function () {
        console.log('mouseover');
    };

    $scope.showProperty = function (evt, data) {
        $scope.store = data;
        console.log(data);
        if ($routeParams.listType.substring(0, 1) == 'S') {
            $scope.store.displayPrice = "Sale Price : RM" + data.SalePrice.toLocaleString();
        } else {
            $scope.store.displayPrice = "Rental Price : RM" + data.RentPrice.toLocaleString();
        }
        $scope.map.showInfoWindow('foo', this);
    };

    $scope.showInfoWindow = function (evt, storeId) {
        //$scope.store = $scope.stores[storeId];
        console.log("google showInfoWindow");
        console.log(this);
        console.log(storeId);
        console.log(evt);
        $scope.map.showInfoWindow('foo', this);
    };

    $scope.isPropertyTransactSale = function (obj) {
        return obj == 'S' ? true : false;
    };

    $scope.isEmpty = function (obj) {
        for (var i in obj) if (obj.hasOwnProperty(i)) return false;
        return true;
    };

    pageInit();
    function pageInit(){ 
        if ($routeParams.id) {
            var data = sessionStorage.getItem($routeParams.id);
            console.log("DATA!!!");
            console.debug("%o", data);
            
            // if no data in session, get from the server
            jsonObj = JSON.parse(data);
            $scope.property = jsonObj;

            var url = 'api/Property/GetPropertyDetailByListType/' + $routeParams.id + '/' + $routeParams.listType;
            $http.get(url).success(function (data) {
                console.debug("%o", data);

                console.log("Success Data: " + JSON.stringify($scope.entireData));
                $scope.property = data.propertyDetail;
                $scope.property.listType = $routeParams.listType;

                var transactStr;
                if ($routeParams.listType.substring(0, 1) == 'S') {
                    $scope.property.transact = 'S';
                    $scope.nearByList = data.nearByListSale;
                    var toBeFilter = $filter('filter')($scope.nearByList, { propertyid: $routeParams.id });
                    var index = $scope.nearByList.indexOf(toBeFilter[0]);
                    $scope.nearByList.splice(index, 1);
                    transactStr = " FOR SALE" + "\n" + "Sale Price : RM" + $scope.property.SalePrice.toLocaleString();
                } else {
                    $scope.property.transact = 'R';
                    $scope.nearByList = data.nearByListRent;
                    transactStr = " FOR RENT" + "\n" + "Rental Price : RM" + $scope.property.RentPrice.toLocaleString();
                }

                $scope.slides = data.pictureList;
                if (!$scope.slides) {
                    $scope.slides = [];
                    $scope.slides.push({ pictureURL: 'http://www.penangrealty.my/images/penang realty,Apartment,house,condominium,terrace house,semi d,detached,bungalow,high end luxury properties,landed properties,gated guarded house.png' });
                }
                
                $scope.shareTitle = data.propertyDetail.ShortHeading;
                $scope.shareDescription = data.propertyDetail.LongHeading;
                $scope.shareURL = BASE_URL + "/property/" + data.propertyDetail.propertyid + "/" + data.propertyDetail.listType;
                $scope.shareImageURL = data.propertyDetail.PropertyURL || "http://www.penangrealty.my/images/penang realty,Apartment,house,condominium,terrace house,semi d,detached,bungalow,high end luxury properties,landed properties,gated guarded house.png";
                $scope.googleShareURL = encodeURI(encodeURI($scope.shareURL));

                $scope.isSearch = false;
            }).error(function (data) {
                console.log("ERROR: " + data);
                $scope.isSearch = false;
            });
        }
    }

    $scope.submitForm = function (obj) {
        var url = 'https://script.google.com/macros/s/AKfycbwcomu5o2CIbkO1lCjyZhDrF_dejSPWi0VAj9knYKYRr3jHDUY/exec';
        $http({
            method: 'POST',
            url: url,
            data: $(obj.target).serialize(),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (data) {
           

            alert("Thanks for Subscribing to our Newsletter!");
            $scope.subscription_email = "";
        });
    }

    $scope.sendMessage = function (email) {
        $scope.data = {};
        $scope.data.Email = email;
        $scope.data.Title = $scope.contact_subject;
        $scope.data.Body = $scope.contact_message + "<br/><br/><br/> This message is sent from Shi Jie Property Website:<br/>Name: " + $scope.contact_name + "<br/>Email: " + $scope.contact_email;
        //$scope.data.Body = "Hi,<br/><br/>Check out this property from Shijieproperty: <a href=" + +">Here</a><br/><br/>Thank You."


        $http.post('/api/User/sendEmail', $scope.data, {
            headers: { 'Content-Type': 'application/json;charset=UTF-8' }
        }).success(function (response) {
            alert("Thanks for submitting, we will contact you soon.");
            $scope.contact_name = "";
            $scope.contact_email = "";
            $scope.contact_subject = "";
            $scope.contact_message = "";
        }).error(function (error) {
            alert("Ops! There is an internal server error problem. Please try again later.");
            console.log("ERROR" + +JSON.stringify(error))
        });
    }

});